package org.jeecg.modules.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.crm.entity.ProjectScheme;
import org.jeecg.modules.crm.entity.UnitType;
import org.jeecg.modules.crm.mapper.ProjectSchemeMapper;
import org.jeecg.modules.crm.mapper.UnitTypeMapper;
import org.jeecg.modules.crm.service.IProjectSchemeService;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Commission Scheme
 * @Author: jeecg-boot
 * @Date:   2025-04-08
 * @Version: V1.0
 */
@Service
public class ProjectSchemeServiceImpl extends ServiceImpl<ProjectSchemeMapper, ProjectScheme> implements IProjectSchemeService {

    @Autowired
    UnitTypeMapper unitMapper;
    @Autowired
    SysDictMapper systemMapper;
    @Override
    public List<ProjectScheme> queryDictListbymodelid(String modelid) {
        //查询对应的数据
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.ge("main_id",modelid);
        List<ProjectScheme> projectSchemes= baseMapper.selectList(queryWrapper);
        for (ProjectScheme projectScheme : projectSchemes) {
            projectScheme.setDictName("["+projectScheme.getCommission()+"]"+"["+projectScheme.getFromDate()+"-"+projectScheme.getToDate()+"]");
        }
        return projectSchemes;
    }

    @Override
    public void saveProjectScheme(ProjectScheme projectScheme) {
        UnitType unitType = unitMapper.selectById(projectScheme.getUnitType());
        String commission = systemMapper.queryDictTextByKey("Commission", projectScheme.getCommissionType());
        if (commission.equals("RM")){
            commission = "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        projectScheme.setDictName(unitType.getName()+"["+projectScheme.getCommission()+commission+"]"+"["+simpleDateFormat.format(projectScheme.getFromDate())+"-"+simpleDateFormat.format(projectScheme.getToDate())+"]");
        this.save(projectScheme);
    }

    @Override
    public void updateProjectScheme(ProjectScheme projectScheme) {
        UnitType unitType = unitMapper.selectById(projectScheme.getUnitType());
        String commission = systemMapper.queryDictTextByKey("Commission", projectScheme.getCommissionType());
        if (commission.equals("RM")){
            commission = "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        projectScheme.setDictName(unitType.getName()+"["+projectScheme.getCommission()+commission+"]"+"["+simpleDateFormat.format(projectScheme.getFromDate())+"-"+simpleDateFormat.format(projectScheme.getToDate())+"]");
        this.updateById(projectScheme);
    }
}
